package com.tyg.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tyg.entity.IsVip;
import com.tyg.entity.TbVenueInfo;
import com.tyg.entity.TbVenueOrder;
import com.tyg.mapper.IsVipMapper;
import com.tyg.pojo.VenueOrderInfo;
import com.tyg.service.IsVipService;
import com.tyg.service.TbVenueInfoService;
import com.tyg.service.TbVenueOrderService;
import com.tyg.util.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangjingnan
 * @since 2022-11-28
 */
@RestController
@CrossOrigin
@RequestMapping("/tb-venue-order")
public class TbVenueOrderController {
    @Autowired
    TbVenueOrderService tbVenueOrderService;

    @Autowired
    IsVipService isVipService;

    @Autowired
    TbVenueInfoService tbVenueInfoService;

    /**
     * 获取场馆已被使用的时间段
     */
    @PostMapping("/getUsedVenueInfo")
    public ResultCode getVenueInfo(
            Integer venueId,
            String venueDate) {
        TbVenueOrder tbVenueOrder = new TbVenueOrder();
        if (!ObjectUtils.isEmpty(venueId)){
            tbVenueOrder.setVenueId(venueId);
        }
        if (!ObjectUtils.isEmpty(venueDate)){
            tbVenueOrder.setVenueDate(venueDate);
        }else {
            Date date = new Date();
            SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");
            tbVenueOrder.setVenueDate(dateFormat.format(date));
        }
        List<String> UsedVenueTimes = tbVenueOrderService.getUsedVenueInfo(tbVenueOrder);
        return ResultCode.success(UsedVenueTimes);
    }

    /**
     * 更新预定场馆订单状态
     */
    @PostMapping("/updateVenueOrderStatus")
    public ResultCode updateVenueOrderStatus(
            Integer id,
            Integer orderState) {
        TbVenueOrder tbVenueOrder = new TbVenueOrder();
        if (!ObjectUtils.isEmpty(id)){
            tbVenueOrder.setId(id);
        }
        if (!ObjectUtils.isEmpty(orderState)){
            tbVenueOrder.setOrderState(orderState);
        }
        tbVenueOrderService.updateVenueOrderStatus(tbVenueOrder);
        return ResultCode.success();
    }

    /**
     * 添加场馆预定订单
     */
    @PostMapping("/insertVenueOrder")
    public ResultCode insertVenueOrder(
            Integer venueId,
            String venueDate,
            String venueTime,
            Integer orderUser,
            Integer orderState,
            String venuePrice) {
        if (ObjectUtils.isEmpty(orderUser)){
            return ResultCode.error("参数错误！");
        }
        IsVip isVip = isVipService.selectByWorkId(orderUser);
        // 顾客需要支付的费用
        double needMoney = venueTime.split(",").length * Double.parseDouble(venuePrice);
        if (isVip.getMoney() > needMoney){
            TbVenueOrder tbVenueOrder = new TbVenueOrder();
            if (!ObjectUtils.isEmpty(venueId)){
                tbVenueOrder.setVenueId(venueId);
            }
            if (!ObjectUtils.isEmpty(venueDate)){
                tbVenueOrder.setVenueDate(venueDate);
            }
            if (!ObjectUtils.isEmpty(venueTime)){
                StringBuilder venueTimes = new StringBuilder();
                for (String time : venueTime.split(",")) {
                    venueTimes.append(time.split("~")[0]);
                    venueTimes.append(",");
                }
                tbVenueOrder.setVenueTime(venueTimes.substring(0,venueTimes.length() - 1));
            }
            if (!ObjectUtils.isEmpty(orderUser)){
                tbVenueOrder.setOrderUser(orderUser);
            }
            if (!ObjectUtils.isEmpty(orderState)){
                tbVenueOrder.setOrderState(orderState);
            }
            isVipService.updateMoney(0 - needMoney,orderUser);
            tbVenueOrderService.insertNewOrder(tbVenueOrder);
            return ResultCode.success("预定成功");
        }else {
            return ResultCode.success("余额不足，请充值！");
        }
    }

    /**
     * 添加场馆预定订单
     */
    @PostMapping("/getAllVenueOrder")
    public ResultCode getAllVenueOrder(
            @RequestParam(defaultValue = "1" ,required = true)int pageIndex, Integer pageSize
    ){
        PageHelper.startPage(pageIndex, pageSize);
        List<VenueOrderInfo> allVenueOrder = tbVenueOrderService.getAllVenueOrder(new TbVenueOrder());
        PageInfo<VenueOrderInfo> page= new PageInfo<>(allVenueOrder);
        return ResultCode.success(page);
    }

    /**
     * 取消预定
     */
    @PostMapping("/cancelVenueOrder")
    public ResultCode cancelVenueOrder(
            String id,
            String venueId,
            String venueTime,
            Integer orderUser,
            String cause,
            String venuePrice) {
        try {
        // 需要退还顾客的费用
            double returnMoney = venueTime.split(",").length * Double.parseDouble(venuePrice);
            isVipService.updateMoney(returnMoney,orderUser);
            TbVenueOrder tbVenueOrder = new TbVenueOrder();
            tbVenueOrder.setId(Integer.parseInt(id));
            tbVenueOrder.setOrderState(4);
            tbVenueOrder.setReturnCause(cause);
            tbVenueOrderService.updateVenueOrderStatus(tbVenueOrder);
            return ResultCode.success("取消成功");
        }catch (Exception e){
            return ResultCode.success(e.getStackTrace());
        }
    }
}

